module Server =
struct

  let boards = [|
    (* got these from Gnome Sudoku *)
    "500640908002000000900002000800320010040105080060078005000200009000000700304089006";
    "071600900400000301000802070050000400200105003003000090010204000608000002004009150";
    "076305908001000000002001030000510700200000005003078000090400300000000500708902160";
    "076005008080000050002061430800010000000603000000070001095480300020000080700900160";
    "076300900380740050902000000009000000010603040000000600000000302020037089008002160";
    "076005010080040006902000400000500020017000840040008000005000302600030080030900160";
    "070025018300009200000060430000004720000000000043200000095080000004100009730950060";
    "000305900380000206950060000009014003000000000500270600000080072604000089008902000";
    "006025008080009006900000400060010023000603000540070090005000002600100080700950100";
    "006005918000740050900060030000500700200000005003008000090080002020037000738900100";
    "000500000351020000600739000907004080200000007040100609000685004000040813000002000";
    "790501000051000006080009002000250081000000000840073000100600070500000810000302065";
    "000060000001000096004039000067004380200806007045100620000680200520000800000010000";
    "000061030000000790004730002060050080203000507040070020100085200026000000070310000";
    "000001438000000096000009102000004301200806007805100000109600000520000000478300000";
    "902800400010300020000604000400180050200000009070056008000703000080009070007008906";
    "090000200080100540000039080003048900000703000009520700050270000031005070004000020";
    "490000010080002500105000600700640000500703008000021004008000301001900070070000025";
    "809000030050470000070100006032007901000000000104900350500002010000048090060000402";
    "009200004006003100400100500030800901000304000104006050007002003001700600900001400";
    "009060000206400100003180500002000001790000068100000300007092800001008605000030400";
    "000050490700200003000609200905070004300000007400060301002405000600008009094030000";
    "000000098000004100843600200905003000320000087000900301002005736007100000590000000";
    "200357000709200060000000000900000024006541900470000001000000000030008509000736002";
    "001057400009200000840600070005003020300000007070900300080005036000008500004730800";
    "260300090700204060000009000905070604000000000408060301000400000030108009090006012";
    "047010520000009000008060003070000940400801002069000080600040100000100000014020650";
    "001520790040000050300008100000200807000309000107006000005900002070000060028015300";
    "001004703700060050002708006090040000000309000000050020600907400070080001408600300";
    "001000090700103050000098106090040000084000610000050020605930000070402001020000300";
    "800024000709100000050090040006000830204000605037000900010030080000002501000610009";
    "001020000009063000300008140090000830004070600037000020015900002000480500000010300";
    "060504703009000050302090000506000800080000010007000904000030402070000500408605070";
    "000020090000063008300008140000040807084000610107050000015900002900480000020010000";
    "801004000000060050302700006090040007200309005100050020600007402070080000000600309";
    "000000000030150002400009670050610000006927100000043090083400001900076040000000000";
    "003008050000019400050630000201007005900000004800200106000024030002390000040800900";
    "090370005300004091040100060000009000400605002000800000020007030180400007700083010";
  |]

  let get_board () =
    let board = boards.(Random.int (Array.length boards)) in
    Array.init 9 (fun i ->
      Array.init 9 (fun j ->
        match board.[i * 9 + j] with
          | '0' -> None
          | n -> Some (Char.code n - 48)))

end

let start() =
  let (opt_list, cmdline_cfg) = Netplex_main.args() in

  Arg.parse
    opt_list
    (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
    "usage: netplex [options]";

  let config_cgi = {
    Netcgi_env.default_config with
      Netcgi_env.permitted_input_content_types = "text/plain" ::
      Netcgi_env.default_config.Netcgi_env.permitted_input_content_types
  } in

  let module M = Proto_js_srv.Sync(Server) in

  let factories =
    [ Nethttpd_plex.nethttpd_factory
        ~config_cgi
        ~handlers:["sudoku", Orpc_js_server.service M.handler]
        ();
    ]
  in

  Netplex_main.startup
    (Netplex_mp.mp())
    Netplex_log.logger_factories   (* allow all built-in logging styles *)
    Netplex_workload.workload_manager_factories (* ... all ways of workload management *)
    factories
    cmdline_cfg
;;

Sys.set_signal Sys.sigpipe Sys.Signal_ignore;
start()
;;
